package com.arvato.admin01.service.impl;

import com.arvato.admin01.Util.JsonUtil;
import com.arvato.admin01.component.RedisLock;
import com.arvato.admin01.service.order.RedisOrderService;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class RedisOrderServiceImpl implements RedisOrderService {

    private RedisLock redisLock;
    //商品详情
    private static HashMap<String, Integer> product = new HashMap();
    //订单表
    private static HashMap<String, String> orders = new HashMap();
    //库存表
    private static HashMap<String, Integer> stock = new HashMap();

    static {
        product.put("123", 10000);
        stock.put("123", 10000);
    }

    @Override
    public String select_info(String product_id) {
        return "限量抢购商品XXX共" + product.get(product_id) + ",现在成功下单" + orders.size()
                + ",剩余库存" + stock.get(product_id) + "件";
    }

    @Override
    public String order1(String product_id) throws  Exception {
        /**
         * redis加锁
         */
        String value = System.currentTimeMillis() + 10000 + "";
        if (!redisLock.lock1(product_id, value)) {
            //系统繁忙，请稍后再试
            throw new Exception("当前业务繁忙，请稍后再试");
        }
        //##############################业务逻辑#################################//
        if (stock.get(product_id) == 0) {
            return "活动已经结束了";
            //已近买完了
        } else {
            //还没有卖完
            try {
                //模拟操作数据库
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            orders.put(JsonUtil.getuuid(), product_id);
            stock.put(product_id, stock.get(product_id) - 1);
        }
        //##############################业务逻辑#################################//
        /**
         * redis解锁
         */
        redisLock.unlock(product_id, value);
        return select_info(product_id);
    }
}
